package com.ijunfu.lambda._10_avg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @Title  : 帅选出集合中的偶数，并求其平均值
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/11/18 14:59
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
public class Main {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> evenList = new ArrayList<>();
        int sum = 0;
        for(Integer num: list) {
            if (num%2==0) {
                evenList.add(num);
                sum+=num;
            }
        }
        System.out.println(sum/evenList.size());

        Double collect = list.stream().filter(e -> e % 2 == 0).collect(Collectors.averagingInt(Integer::intValue));
        System.out.println(collect);
    }
}
